package com.ld.security.user;

import com.ld.security.auth.AuthenticationService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/api/form/v1/user")
@RequiredArgsConstructor
public class UserFormController {

    private final AuthenticationService service;
    private final AuthenticationManager authManager;

    @PostMapping("/authenticate")
    public String authenticate(FormLoginRequest request) {
        Authentication authentication = authManager.authenticate(
                new UsernamePasswordAuthenticationToken(request.username, request.password)
        );
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return "redirect:/page/home";
    }

    public record FormLoginRequest(String username, String password) {
    }
}
